#! /usr/bin/python3
# _*_ cding:UTF-8 _*_
"""
    每次调用 返回值加1
"""


def createCounter1():
    count = 0

    def counter():
        nonlocal count     # 声明此变量是外一层的同一变量
        count += 1
        return count
    return counter


def createCounter2():
    def num_generator():
        """
            生成器
        """
        num = 0
        while True:
            num += 1
            yield num

    int_num = num_generator()

    def counter():
        """
        返回生成器的next()调用
        """
        return next(int_num)

    return counter


if __name__ == '__main__':
    counterA = createCounter1()
    print(counterA(), counterA(), counterA(), counterA(), counterA())        # 1 2 3 4 5
    counterB = createCounter1()
    if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
        print('测试通过!')
    else:
        print('测试失败!')
    counterA = createCounter2()
    print(counterA(), counterA(), counterA(), counterA(), counterA())        # 1 2 3 4 5
    counterB = createCounter2()
    if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
        print('测试通过!')
    else:
        print('测试失败!')
